
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>第&nbsp;12&nbsp;章&nbsp;SOAP Web 服务</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="../toc/index.html" title="Dive Into Python">
      <link rel="previous" href="../http_web_services/summary.html" title="11.10.&nbsp;小结">
      <link rel="next" href="install.html" title="12.2.&nbsp;安装 SOAP 库">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<span class="thispage">SOAP Web 服务</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="../http_web_services/summary.html" title="上一页: “小结”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="install.html" title="下一页: “安装 SOAP 库”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="chapter" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="soap"></a>第&nbsp;12&nbsp;章&nbsp;<span class="acronym">SOAP</span> Web 服务
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="toc">
            <ul>
               <li><span class="section"><a href="index.html#soap.divein">12.1. 概览</a></span></li>
               <li><span class="section"><a href="install.html">12.2. 安装 SOAP 库</a></span><ul>
                     <li><span class="section"><a href="install.html#d0e30152">12.2.1. 安装 PyXML</a></span></li>
                     <li><span class="section"><a href="install.html#d0e30255">12.2.2. 安装 fpconst</a></span></li>
                     <li><span class="section"><a href="install.html#d0e30353">12.2.3. 安装 SOAPpy</a></span></li>
                  </ul>
               </li>
               <li><span class="section"><a href="first_steps.html">12.3. 步入 SOAP</a></span></li>
               <li><span class="section"><a href="debugging.html">12.4.  SOAP 网络服务查错</a></span></li>
               <li><span class="section"><a href="wsdl.html">12.5. WSDL 介绍</a></span></li>
               <li><span class="section"><a href="introspection.html">12.6. 以 WSDL 进行 SOAP 内省</a></span></li>
               <li><span class="section"><a href="google.html">12.7. 搜索 Google</a></span></li>
               <li><span class="section"><a href="troubleshooting.html">12.8.  SOAP 网络服务故障排除</a></span></li>
               <li><span class="section"><a href="summary.html">12.9. 小结</a></span></li>
            </ul>
         </div>
         <div class="abstract">
            <p><a href="../http_web_services/index.html">第 11 章</a> 关注 HTTP 上面向文档的web 服务。“<span class="quote">输入参数</span>” 是 <span class="acronym">URL</span>，“<span class="quote">返回值</span>” 是需要你来解析的一个实际的 XML 文档。
            </p>
            <p>本章将关注更加结构化的 <span class="acronym">SOAP</span> web 服务。<span class="acronym">SOAP</span> 不需要你直接与 HTTP 请求和 XML 文档打交道，而是允许你模拟返回原始数据类型的函数调用。正像你将要看到的，这个描述恰如其份；你可以使用标准 <span class="application">Python</span> 调用语法通过 <span class="acronym">SOAP</span> 库去调用一个函数，这个函数也自然会返回 <span class="application">Python</span> 对象和值。但揭开这层面纱，<span class="acronym">SOAP</span> 库实际上执行了一个多个 XML 文档和远程服务器参与的复杂处理过程。
            </p>
            <p><span class="acronym">SOAP</span> 的贴切定义很复杂，不要误认为 <span class="acronym">SOAP</span> 就是用于调用远程函数。有些人觉得应该补充上：<span class="acronym">SOAP</span> 还允许单向异步的信息通过，以及面向文档的 Web 服务。有这样想法的人是正确的，<span class="acronym">SOAP</span> 的确是这样，但却不止于此。但这一章的重点在于所谓的 “<span class="quote">RPC-style</span>” <span class="acronym">SOAP</span>――调用远程函数获得返回结果。
            </p>
         </div>
         <div class="section" lang="zh_cn">
            <div class="titlepage">
               <div>
                  <div>
                     <h2 class="title"><a name="soap.divein"></a>12.1.&nbsp;概览
                     </h2>
                  </div>
               </div>
               <div></div>
            </div>
            <div class="abstract">
               <p>你用 Google，对吧？它是一个很流行的搜索引擎。你是否希望能以程序化的方式访问 Google 的搜索结果呢？现在你能做到了。下面是一个用 <span class="application">Python</span> 搜索 Google 的程序。
               </p>
            </div>
            <div class="example"><a name="d0e30081"></a><h3 class="title">例&nbsp;12.1.&nbsp;<tt class="filename">search.py</tt></h3><pre class="programlisting"><span class='pykeyword'>from</span> SOAPpy <span class='pykeyword'>import</span> WSDL

<span class='pycomment'># you'll need to configure these two values;</span>
<span class='pycomment'># see http://www.google.com/apis/</span>
WSDLFILE = <span class='pystring'>'/path/to/copy/of/GoogleSearch.wsdl'</span>
APIKEY = <span class='pystring'>'YOUR_GOOGLE_API_KEY'</span>

_server = WSDL.Proxy(WSDLFILE)
<span class='pykeyword'>def</span><span class='pyclass'> search</span>(q):
    <span class='pystring'>"""Search Google and return list of {title, link, description}"""</span>
    results = _server.doGoogleSearch(
        APIKEY, q, 0, 10, False, <span class='pystring'>""</span>, False, <span class='pystring'>""</span>, <span class='pystring'>"utf-8"</span>, <span class='pystring'>"utf-8"</span>)
    <span class='pykeyword'>return</span> [{<span class='pystring'>"title"</span>: r.title.encode(<span class='pystring'>"utf-8"</span>),
             <span class='pystring'>"link"</span>: r.URL.encode(<span class='pystring'>"utf-8"</span>),
             <span class='pystring'>"description"</span>: r.snippet.encode(<span class='pystring'>"utf-8"</span>)}
            <span class='pykeyword'>for</span> r <span class='pykeyword'>in</span> results.resultElements]

<span class='pykeyword'>if</span> __name__ == <span class='pystring'>'__main__'</span>:
    <span class='pykeyword'>import</span> sys
    <span class='pykeyword'>for</span> r <span class='pykeyword'>in</span> search(sys.argv[1])[:5]:
        <span class='pykeyword'>print</span> r[<span class='pystring'>'title'</span>]
        <span class='pykeyword'>print</span> r[<span class='pystring'>'link'</span>]
        <span class='pykeyword'>print</span> r[<span class='pystring'>'description'</span>]
        print</pre></div>
            <p>你可以在较大的程序中以模块导入并使用它，也可以在命令行上运行这个脚本。在命令行上，需要把查询字符串作为命令行参数使用，之后就会打印出最前面的五个 Google 查询结果，包括：URL、标题和描述信息。</p>
            <p>下面是以 “<span class="quote">python</span>” 作为命令行参数的查询结果。
            </p>
            <div class="example"><a name="d0e30094"></a><h3 class="title">例&nbsp;12.2.&nbsp;<tt class="filename">search.py</tt> 的使用样例
               </h3><pre class="screen">
<tt class="prompt">C:\diveintopython\common\py&gt;</tt> <span class="userinput">python search.py <span class='pystring'>"python"</span></span>
<span class="computeroutput">&lt;b&gt;Python&lt;/b&gt; Programming Language
http://www.python.org/
Home page for &lt;b&gt;Python&lt;/b&gt;, an interpreted, interactive, object-oriented,
extensible&lt;br&gt; programming language. &lt;b&gt;...&lt;/b&gt; &lt;b&gt;Python&lt;/b&gt;
is OSI Certified Open Source: OSI Certified.

&lt;b&gt;Python&lt;/b&gt; Documentation Index
http://www.python.org/doc/
 &lt;b&gt;...&lt;/b&gt; New-style classes (aka descrintro). Regular expressions. Database
API. Email Us.&lt;br&gt; docs@&lt;b&gt;python&lt;/b&gt;.org. (c) 2004. &lt;b&gt;Python&lt;/b&gt;
Software Foundation. &lt;b&gt;Python&lt;/b&gt; Documentation. &lt;b&gt;...&lt;/b&gt;

Download &lt;b&gt;Python&lt;/b&gt; Software
http://www.python.org/download/
Download Standard &lt;b&gt;Python&lt;/b&gt; Software. &lt;b&gt;Python&lt;/b&gt; 2.3.3 is the
current production&lt;br&gt; version of &lt;b&gt;Python&lt;/b&gt;. &lt;b&gt;...&lt;/b&gt;
&lt;b&gt;Python&lt;/b&gt; is OSI Certified Open Source:

Pythonline
http://www.pythonline.com/


Dive Into &lt;b&gt;Python&lt;/b&gt;
http://diveintopython.org/
Dive Into &lt;b&gt;Python&lt;/b&gt;. &lt;b&gt;Python&lt;/b&gt; from novice to pro. Find:
&lt;b&gt;...&lt;/b&gt; It is also available in multiple&lt;br&gt; languages. Read
Dive Into &lt;b&gt;Python&lt;/b&gt;. This book is still being written. &lt;b&gt;...&lt;/b&gt;</span>
</pre></div>
            <div class="furtherreading">
               <h3>进一步阅读</h3>
               <ul>
                  <li><a href="http://www.xmethods.net/">http://www.xmethods.net/</a> 是一个访问 <span class="acronym">SOAP</span> web 服务的公共知识库。
                  </li>
                  <li><a href="http://www.w3.org/TR/soap/"><span class="acronym">SOAP</span> 规范</a>相当可读，如果你喜欢这类东西的话。
                  </li>
               </ul>
            </div>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="../http_web_services/summary.html">&lt;&lt;&nbsp;小结</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<span class="thispage">1</span> <span class="divider">|</span> <a href="install.html" title="12.2.&nbsp;安装 SOAP 库">2</a> <span class="divider">|</span> <a href="first_steps.html" title="12.3.&nbsp;步入 SOAP">3</a> <span class="divider">|</span> <a href="debugging.html" title="12.4.&nbsp; SOAP 网络服务查错">4</a> <span class="divider">|</span> <a href="wsdl.html" title="12.5.&nbsp;WSDL 介绍">5</a> <span class="divider">|</span> <a href="introspection.html" title="12.6.&nbsp;以 WSDL 进行 SOAP 内省">6</a> <span class="divider">|</span> <a href="google.html" title="12.7.&nbsp;搜索 Google">7</a> <span class="divider">|</span> <a href="troubleshooting.html" title="12.8.&nbsp; SOAP 网络服务故障排除">8</a> <span class="divider">|</span> <a href="summary.html" title="12.9.&nbsp;小结">9</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="install.html">安装 SOAP 库&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>